import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 登录功能
 */
public class JDBCLogin {
    public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection();){
            final Statement statement = connection.createStatement();
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入用户名:");
            String scusername=sc.nextLine();
            System.out.println("请输入密码:");
            String scpassword=sc.nextLine();
            String sql="select username,password from userinfo where username='"+scusername+"' and password ='"+scpassword+"'  ";
            final ResultSet resultSet = statement.executeQuery(sql);
            /*
                ResultSet对象:获取查询语句的执行结果集;
                需要遍历,一条一条数据获取;
                重要方法: resultSet.next()
                返回值:true : 如果存在下一个元素,返回true
                      false: 如果不存在下一个元素,返回false
             */
            if (resultSet.next()){
                final String username = resultSet.getString("username");
                System.out.println("登录成功:"+username);
            }else {
                System.out.println("不好意思哦登录失败");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
